package com.mystudy.leetcode.contest.p_5140;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author 周志龙
 * @Email 15216876513@163.com
 * @Date on 2019/7/28 11:09
 * @Description 字母板上的路径
 */
public class Solution {
    private static final Logger LOGGER = LoggerFactory.getLogger(Solution.class);

    public static void main(String[] args) {
        String leet = alphabetBoardPath("zdz");
        LOGGER.debug("leet = [{}]", leet);
    }

    public static String alphabetBoardPath(String target) {
        char curIndex = 'a';
        String route = "";
        for (int i = 0; i < target.length(); i++) {
            char c = target.charAt(i);
            if(c == curIndex){
                route+="!";
                continue;
            }
            char temp  =c;
            if (c == 'z') {
                c -= 5;
            }
            int level = getLevel(c);
            int curLevel = getLevel(curIndex);
            int m = Math.abs(curLevel - level);
            if (m != 0) {
                if (curLevel > level) {
                    for (int j = 0; j < m; j++) {
                        route += "U";
                        curIndex -= 5;
                    }
                } else {
                    for (int j = 0; j < m; j++) {
                        route += "D";
                        curIndex += 5;
                    }
                }
            }
            int n = Math.abs(curIndex - c);
            if (n != 0) {
                if (curIndex > c) {
                    for (int j = 0; j < n; j++) {
                        route += "L";
                    }
                } else {
                    for (int j = 0; j < n; j++) {
                        route += "R";
                    }
                }
                curIndex = c;
            }
            if(temp == 'z' && curIndex != 'z'){
                route+='D';
                curIndex = 'z';
            }
            route += "!";
        }
        return route;
    }
    private static int getLevel(char c) {
        if (c >= 'a' && c <= 'e') {
            return 1;
        } else if (c >= 'f' && c <= 'j') {
            return 2;
        } else if (c >= 'k' && c <= 'o') {
            return 3;
        } else if (c >= 'p' && c <= 't') {
            return 4;
        } else if (c >= 'u' && c <= 'y') {
            return 5;
        } else {
            return 6;
        }
    }
}
